package function;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.ImageObserver;

import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import dialog.AddPerson;
import panel.Window;

public class Login implements Function, ActionListener{

	/*Panel*/
	Window window;
	
	/*component*/
	JTextField id;
	JPasswordField pwd;
	
	/*Font*/
	Font fontBold, fontBase;
	
	public Login(Window window){
		this.window = window;
	
		/*Font*/
		fontBold = new Font("",1,12);
		fontBase = new Font("",0,12);
		
		/*Component*/
		id = new JTextField(12);
		pwd = new JPasswordField(12);
		id.setBounds(67, 75, 122, 22);
		pwd.setBounds(67, 107, 122, 22);
		window.add(id);
		window.add(pwd);
		
		/* Listener */
		id.addActionListener(this);
		pwd.addActionListener(this);
	}
	
	public JTextField getId() {
		return id;
	}


	public JPasswordField getPwd() {
		return pwd;
	}

	@SuppressWarnings("deprecation")
	public void getConnect(){
		/* Admin */
		if (id.getText().equals("root") && pwd.getText().equals("1234")) {
			window.createAdmin();
			window.setStateLogin(true);
		}
		/*User*/
		//We don't make it yet
		/*Other*/
		else{
			JOptionPane.showMessageDialog(null, "ID or Password is not correct!", "Error",
					JOptionPane.ERROR_MESSAGE);
		}
	}
	
	public void addPerson(){
		new AddPerson(window.getMain(), 430, window.getFileManagement());
	}
	
	public void editPerson(){
		if(window.isStateLogin()){
			
		}
		else{
			JOptionPane.showMessageDialog(null, "Please, Login first!", "Error",
					JOptionPane.ERROR_MESSAGE);
		}
	}
	
	public void deletePerson(){
if(window.isStateLogin()){
			
		}
		else{
			JOptionPane.showMessageDialog(null, "Please, Login first!", "Error",
					JOptionPane.ERROR_MESSAGE);
		}
	}
	
	public void searchPerson(){
		
	}
	
	public void draw(Graphics bufferG, Toolkit winTool, ImageObserver ob){
		if(window.isStateLogin()){

			bufferG.setColor(Color.white);
			bufferG.setFont(fontBold);
			bufferG.drawString(window.getPerson().getName(), 70, 65);
			bufferG.setFont(fontBase);
			bufferG.drawString("dear", 31, 65);
			
			bufferG.drawImage(winTool.getImage(".\\Images\\Main_LogoutButton"+window.getStateButtonLogout()+".png"), 153, 144, ob);
		}
		else{
			bufferG.drawImage(winTool.getImage(".\\Images\\Main_LoginText.png"), 31, 49, ob);
			bufferG.drawImage(winTool.getImage(".\\Images\\Main_LoginButton"+window.getStateButtonLogin()+".png"), 153, 144, ob);
		}
		bufferG.drawImage(winTool.getImage(".\\Images\\Main_LoginSimpleButton.png"), 31, 147, ob);
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}
}
